package org.fullhappy.rest.entities;

import java.util.ArrayList;
import java.util.List;

import org.fullhappy.dao.CategoryDAO;
import org.fullhappy.dao.GalleryDAO;
import org.fullhappy.dao.ProductDAO;
import org.fullhappy.entity.Category;
import org.fullhappy.entity.Gallery;
import org.fullhappy.entity.Product;
import org.fullhappy.model.MenuItem;
import org.fullhappy.rest.annotation.GET;
import org.fullhappy.rest.annotation.Path;
import org.fullhappy.rest.annotation.PathParam;
import org.fullhappy.rest.annotation.Produces;

import com.google.api.server.spi.response.BadRequestException;
import com.google.gson.Gson;

@Path("/resources")
public class Resources {
	
	@GET
	@Path("/menu")
	@Produces("application/json")
	public String menu() {
		
	GalleryDAO galleryDAO = new GalleryDAO();
	CategoryDAO categoryDAO = new CategoryDAO();
	List<Gallery> galleries =galleryDAO.findAll();
	List<Category> categories;
	ArrayList<MenuItem> categories_ = new ArrayList<MenuItem>();
	ArrayList<MenuItem> galleries_ = new ArrayList<MenuItem>();
	MenuItem gallery_;
	MenuItem category_;
	if(isAdmin()){
		for (Gallery gallery : galleries) {
			categories = categoryDAO.findByGalleryId(gallery.getId());
			categories_ = new ArrayList<MenuItem>();
			for (Category category : categories) {
				category_ = new MenuItem(category.getId()+"" ,category.getName(), "", null);
				categories_.add(category_);
			}
//			category_ = new MenuItem(null, "Add New Item", "javascript:(new Menu()).openLightBoxAddCategory("+gallery.getId()+");", null);
//			categories_.add(category_);
			
			gallery_ = new MenuItem(gallery.getId()+"", gallery.getName(), "", categories_);
			galleries_.add(gallery_);
		}
//		gallery_ = new MenuItem(null, "Add New", "javascript:(new Menu()).openLightBoxAddGallery();", null);
//		galleries_.add(gallery_);
	}else{
		for (Gallery gallery : galleries) {
			categories = categoryDAO.findByGalleryId(gallery.getId());
			categories_ = new ArrayList<MenuItem>();
			for (Category category : categories) {
				category_ = new MenuItem(category.getId()+"", category.getName(), "/show_room/"+category.getId(), null);
				categories_.add(category_);
			}
			gallery_ = new MenuItem(gallery.getId()+"",gallery.getName(), "", categories_);
			galleries_.add(gallery_);
		}
	}
	MenuItem[] menuItems = new MenuItem[]{new MenuItem(null,"Trang Chủ", "/", null),new MenuItem(null, "Sản Phẩm" ,"javascript:void(0)",galleries_),
			 /*new MenuItem(null, "Giới Thiệu", "/", null),*/ new MenuItem(null, "Liên Hệ", "/contact.jsp", null)};
	
	
	
	Gson gson = new Gson();
	String s = gson.toJson(menuItems);
	/*
		 String s= ""+
	        		"		[" +
	        		"			 {'title':'hone','link':'/'},                                                                                                                                                                                                                                                                                       "+
	        	    "            {'title':'product','link':'javascript:void(0)','childs':                                                                                                                                                                                                                                                                                   "+
	        	    "            	[                                                                                                                                                                                                                                                                                                                    "+
	        	    "            	 {'title':'Product1','link':'javascript:void(0)','childs':[{'title':'Product11','link':'/show_room/2'},{'title':'Product12','link':'/show_room/2'},{'title':'Product13','link':'/show_room/2'},{'title':'Product14','link':'/show_room/2'},{'title':'+ Add New Item','link':'javascript:(new Menu()).openLightBoxAddCategory(1);'}]},                                 "+
	        	    "            	 {'title':'Product2','link':'javascript:void(0)','childs':[{'title':'Product21','link':'/show_room/2'},{'title':'Product22','link':'/show_room/2'},{'title':'+ Add New Item','link':'javascript:(new Menu()).openLightBoxAddCategory(1);'}]},                                                                                                                                 "+
	        	    "            	 {'title':'Product3','link':'javascript:void(0)','childs':[{'title':'Product31','link':'/show_room/2'},{'title':'Product32','link':'/show_room/2'},{'title':'Product33','link':'/show_room/2'},{'title':'Product34','link':'/show_room/2'},{'title':'Product35','link':'/show_room/2'},{'title':'+ Add New Item','link':'javascript:(new Menu()).openLightBoxAddCategory(1);'}]},   "+
	        	    "            	 {'title':'Product4','link':'javascript:void(0)','childs':[{'title':'Product41','link':'/show_room/2'},{'title':'+ Add New Item','link':'javascript:(new Menu()).openLightBoxAddCategory(1);'}]},                                                                                                                                                                     "+
	        	    "            	 {'title':'Product5','link':'javascript:void(0)','childs':[{'title':'Product51','link':'/show_room/2'},{'title':'Product52','link':'/show_room/2'},{'title':'+ Add New Item','link':'javascript:(new Menu()).openLightBoxAddCategory(1);'}]},                                                                                                                                                                                   "+
	        	    "            	 {'title':'Product6','link':'javascript:void(0)','childs':[{'title':'Product61','link':'/show_room/2'},{'title':'Product62','link':'/show_room/2'},{'title':'+ Add New Item','link':'javascript:(new Menu()).openLightBoxAddCategory(1);'}]},                                                                                                                                     "+
	        	    "            	 {'title':'Add New ','link':'javascript:(new Menu()).openLightBoxAddGallery();','childs':[{'title':'--','link':'#'},{'title':'--','link':'#'}]}                                                                                                                      "+
	        	    "            	]                                                                                                                                                                                                                                                                                                                    "+
	        	    "			},                                                                                                                                                                                                                                                                                                                       "+
	        	    "            {'title':'Sale','link':'/sale.html'},                                                                                                                                                                                                                                                                                    "+
	        	    "            {'title':'About Us','link':'/about_us.html'},                                                                                                                                                                                                                                                                                "+
	        	    "            {'title':'Contact Us','link':'#','childs':[{'title':'Address','link':'#'},{'title':'Send Email','link':'#'}]}]                                                                                                                                                                                                              "+
	        		"";
 */
		return s;
 
	}


	@GET
	@Path("/product/{productId}")
	@Produces("application/json")
	public String getProductInfo(@PathParam("productId") long productId) {
		
		ProductDAO productDAO = new ProductDAO();
		Product product = productDAO.find(productId);
		Gson gson = new Gson();
		String s = gson.toJson(product);
		return s ;
 /*
		 String s= "{"+
	        		"	'title' : '�?ầm ren tay con duyên dáng MS 122',											"+				
					"	'description' : '- �?ầm được may bằng chất liệu phi bóng nhập khẩu, m�?m mại, thoáng      "+
					"					mát, mịn màng. <br> - Kiểu dáng thanh lịch, kết hợp kiểu cánh           "+
					"					dơi với đầm tôn vẻ sang tr�?ng của bạn gái. <br> - Màu sắc:              "+
					"					nhi�?u màu. <br> - �?ầm free size, thích hợp cho các bạn gái từ           "+
					"					55kg trở xuống. <br> - �?ầm phi sẽ giúp bạn dịu dàng khi dạo             "+
					"					phố, thanh lịch khi đi làm, sang tr�?ng khi đi dự tiệc. <br> -           "+
					"					Có thể mua để làm quà tặng ý nghĩa dành cho các bạn gái	',			    "+		
					"	'price':'499.000đ',																		"+
					"	'images':['http://www.vatgia.com/pictures_fullsize/lku1300533839.jpg',					"+					
					"	          'http://www.vatgia.com/pictures_fullsize/dyq1301283412.jpg',					"+					
					"	          'http://www.vatgia.com/pictures_fullsize/xjq1301931051.jpg',					"+					
					"	          'http://www.vatgia.com/pictures_fullsize/iaw1304662568.jpg',					"+					
					"	          'http://www.vatgia.com/pictures_fullsize/fqu1304663118.jpg',					"+					
					"	          'http://www.vatgia.com/pictures_fullsize/fqu1304663118.jpg',					"+
					"	          'http://www.vatgia.com/pictures_fullsize/fqu1304663118.jpg']					"+					
					"}";
 
		 return s;
 */
	}
	
	@GET
	@Path("/gallery/{categoryId}/{index}")
	@Produces("application/json")
	public String gallery(@PathParam("categoryId") long categoryId, @PathParam("index") int index) throws BadRequestException {

		ProductDAO productDAO = new ProductDAO();
		List<Product> products = productDAO.findByCategoryId(categoryId);
		if(index*20 < products.size()){
			products = products.subList(index*20, ((index+1)*20) < products.size()?(index+1)*20:products.size());
			Gson gson = new Gson();
			String s = gson.toJson(products);
			return s;
		}else{
			return "[]";
		}
		/*
		 String s= " [{'id' : '23', 'title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/basketball.jpg'}, "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/beachtree.jpg'},   "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/cupcackes.jpg'},   "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/hangmat.jpg'},     "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/new_york.jpg'},    "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/laundry.jpg'},     "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/mom_son.jpg'},     "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/paris.jpg'},       "+
					" {'id' : '23', 'title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/basketball.jpg'}, "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/beachtree.jpg'},   "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/cupcackes.jpg'},   "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/hangmat.jpg'},     "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/new_york.jpg'},    "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/laundry.jpg'},     "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/mom_son.jpg'},     "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/paris.jpg'},       "+
					" {'id' : '23', 'title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/basketball.jpg'}, "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/beachtree.jpg'},   "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/cupcackes.jpg'},   "+
					"{'id' : '23','title' : 'Footer content','price' : '2500 VND','image' : 'http://caroufredsel.dev7studios.com/examples/images/small/hangmat.jpg'}]       ";
					
		 return s);
 		*/
	}
 
	
	private boolean isAdmin() {
		// TODO Auto-generated method stub
		return true;
	}
	
 }

